package co.fitstart.admin.web.controller.system;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;
import co.fitstart.admin.service.AreaService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.site.Area;

/**
 * 区域管理
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/areas")
@MenuSetting("menu-system")
public class AreaController {
    private static final Logger logger = LoggerFactory.getLogger(AreaController.class);
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Autowired
    private AreaService areaService;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-area-management")
    public ModelAndView list(){
        
        ModelAndView modelAndView = new ModelAndView("system/area/area-list");
        
        modelAndView.addObject("areas", genericDaoSupport.loadAll(Area.class));
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create" , method = RequestMethod.GET)
    @MenuSetting("submenu-area-management")
    public ModelAndView create(){
        
        ModelAndView modelAndView = new ModelAndView("system/area/area-create");
        
        return modelAndView;
    }
    
    /**
     * 
     * @param name
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public @ResponseBody String onCreate(@RequestParam("name") String name){
        
        Result result = new Result();
     
        try{
            areaService.create(name);
            
            result.success().message("新建区域成功");
        } catch (BusinessException e) {
            result.fail().message(e.getMessage());
        } catch(Exception e){
            logger.error("onCreateArea() - ", e); 
            result.fail().message("新建区域失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param areaId
     * @return
     */
    @RequestMapping(value = "/{areaId}/edit" , method = RequestMethod.GET)
    @MenuSetting("submenu-area-management")
    public ModelAndView edit(@PathVariable("areaId") Long areaId){
        
        ModelAndView modelAndView = new ModelAndView("system/area/area-edit");
        
        Area area = genericDaoSupport.load(Area.class, areaId);
        
        modelAndView.addObject("area", area);
        
        if(area != null) {
            modelAndView.addObject("sites", CollectionUtils.extractPropertyList(area.getSites(), "id"));
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param areaId
     * @param name
     * @return
     */
    @RequestMapping(value = "/{areaId}/edit", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@PathVariable("areaId") Long areaId,
                                       @RequestParam("name") String name,
                                       @RequestParam(value="siteIds", required = false) List<Long> siteIds){
        
        Result result = new Result();
     
        try{
            areaService.edit(areaId, name, siteIds);
            
            result.success().message("修改成功");
        } catch(Exception e){
            logger.error("onEditArea() - ", e); 
            result.fail().message("修改失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
}
